package com.k.kc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author k
 */
public class TimeZoneUtils {
    private final static String PATTERN_MS = "yyyy-MM-dd HH:mm:ss.SSS";

    public static void main(String[] args) {
        System.out.println("====== Time Test ======");

        // 当前时间 UTC 毫秒数
        long UTCMs = System.currentTimeMillis();

        // 当前时间  UTC 毫秒数
        long UTCMs2 = Clock.systemUTC().millis();

        // 当前时间 UTC 毫秒数
        long UTCMs3 = java.util.Calendar.getInstance().getTimeInMillis();
        System.out.println(UTCMs);
        System.out.println(UTCMs2);
        System.out.println(UTCMs3);


        // 当前时间 带默认时区
        System.out.println(Clock.systemDefaultZone());
        System.out.println("Default : " + LocalDateTime.now().format(DateTimeFormatter.ofPattern(PATTERN_MS)));
        System.out.println("Default : " + LocalDateTime.now(Clock.systemDefaultZone()).format(DateTimeFormatter.ofPattern(PATTERN_MS)));

        // 当前时间 UTC TIME (Date)
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MS);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println("UTC: "+sdf.format(new Date(System.currentTimeMillis())));

        // 当前时间 UTC Time (LocalDateTime)
        System.out.println("UTC : " + LocalDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(PATTERN_MS)));

        // 当前时间 其他时区
        System.out.println("GMT:+00:00 : " + LocalDateTime.now(ZoneId.of("GMT+00:00")).format(DateTimeFormatter.ofPattern(PATTERN_MS)));
        System.out.println("GMT:+08:00 : " + LocalDateTime.now(ZoneId.of("GMT+08:00")).format(DateTimeFormatter.ofPattern(PATTERN_MS)));
        System.out.println("GMT:+05:30 : " + LocalDateTime.now(ZoneId.of("GMT+05:30")).format(DateTimeFormatter.ofPattern(PATTERN_MS)));
        System.out.println("UTC:+00:00 : " +
                LocalDateTime.now(ZoneId.ofOffset("UTC", ZoneOffset.ofHoursMinutesSeconds(0, 0, 0)))
                        .format(DateTimeFormatter.ofPattern(PATTERN_MS))
        );
        System.out.println("UTC:+05:30 : " +
                LocalDateTime.now(ZoneId.ofOffset("UTC", ZoneOffset.ofHoursMinutesSeconds(5, 5, 0)))
                        .format(DateTimeFormatter.ofPattern(PATTERN_MS))
        );
        System.out.println(ZoneId.getAvailableZoneIds());
        System.out.println(ZoneOffset.getAvailableZoneIds());
        System.out.println(ZonedDateTime.now().format(DateTimeFormatter.ofPattern(PATTERN_MS)));



        // str -> LocalDateTime -> Instant -> Date
        String str = "2021-11-09 09:31:55.604";
        LocalDateTime localDateTime = LocalDateTime.parse(str,DateTimeFormatter.ofPattern(PATTERN_MS));
        Instant instant = localDateTime.toInstant(ZoneOffset.ofHoursMinutesSeconds(8,0,0));
        Date d = Date.from(instant);
        System.out.println("str:"+str);
        System.out.println("str -> LocalDateTime:"+localDateTime);
        System.out.println("LocalDateTime -> Date:"+d);

        System.out.println("===========");

        // str -> Date -> Instant -> LocalDateTime
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(PATTERN_MS);
            Date d1 =  sdf1.parse(str);
            System.out.println("str -> Date:"+d1);
            Instant instant1 = d1.toInstant();
            LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1,ZoneId.of("GMT+08:00"));
            System.out.println("Date -> LocalDateTime:"+localDateTime1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
